package com.situ.meetingRoom.controller;

import com.situ.meetingRoom.entity.Booking;
import com.situ.meetingRoom.entity.Result;
import com.situ.meetingRoom.service.BookingService;
import com.situ.meetingRoom.service.impl.BookingServiceImpl;
import com.situ.meetingRoom.util.BaseServlet;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BookingVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "BookingServlet", value = "/booking")
public class BookingServlet extends BaseServlet {

    private BookingService bookingService = new BookingServiceImpl();
    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //分页
        //获取参数
        String sPage = request.getParameter("page");
        String sLimit = request.getParameter("limit");
        //1-获取参数
        String sBoardroomId = request.getParameter("boardroomId");
        String sUserId = request.getParameter("userId");
        String sStatus = request.getParameter("status");

        Booking booking = new Booking();

        if (sBoardroomId != null && sBoardroomId.length()>0) {
            booking.setBoardroomId(Integer.parseInt(sBoardroomId));
        }
        if (sUserId != null && sUserId.length()>0) {
            booking.setUserId(Integer.parseInt(sUserId));
        }
        if (sStatus != null && sStatus.length()>0) {
            booking.setStatus(Integer.parseInt(sStatus));
        }
        Integer page = null, limit = null;
        try {
            page = Integer.parseInt(sPage);
            limit = Integer.parseInt(sLimit);
        } catch (Exception e) {}
        //判断是否分页
        if (page != null) {
            //分页
            PageInfo pageInfo = bookingService.getForPage(page, limit , booking);
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(pageInfo).toJson());
        } else {
            List<BookingVO> bookings = bookingService.getAll(booking);

            //返回，以JSON格式返回
            response.setContentType("application/json; charset=UTf-8");
            response.getWriter().append(Result.ok(bookings).toJson());
        }
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        try {
            Integer id = Integer.parseInt(sId);

            //2-调用service
            bookingService.remove(id);

            //3-返回数据
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sBoardroomId = request.getParameter("boardroomId");
        String sUserId = request.getParameter("userId");
        String sStartTime = request.getParameter("startTime");
        String time = request.getParameter("time");


        Integer BoardroomId = Integer.parseInt(sBoardroomId);
        Integer UserId = Integer.parseInt(sUserId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        try {
            startTime = simpleDateFormat.parse(sStartTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //2-参数校验

        //3-调用service
        Booking booking = new Booking();
        booking.setBoardroomId(BoardroomId);
        booking.setUserId(UserId);
        booking.setStartTime(startTime);
        booking.setTime(time);
        try {
            bookingService.add(booking);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

    public void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");

        Integer id = Integer.parseInt(sId);

        //2-调用service
       Booking booking = bookingService.getById(id);

        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().append(Result.ok(booking).toJson());


    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1-获取参数
        String sId = request.getParameter("id");
        String sBoardroomId = request.getParameter("boardroomId");
        String sUserId = request.getParameter("userId");
        String sStartTime = request.getParameter("startTime");
        String time = request.getParameter("time");
        Integer BoardroomId = Integer.parseInt(sBoardroomId);
        Integer UserId = Integer.parseInt(sUserId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date startTime = null;
        try {
            startTime = simpleDateFormat.parse(sStartTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer id = Integer.parseInt(sId);

        //2-参数校验

        //3-调用service
        Booking booking = new Booking();
        booking.setId(id);
        booking.setBoardroomId(BoardroomId);
        booking.setUserId(UserId);
        booking.setStartTime(startTime);
        booking.setTime(time);


        try {
            bookingService.edit(booking);
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.ok().toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append(Result.error(e.getMessage()).toJson());
        }
    }

}
